package org.cresource.pay.service.impl;

import org.cresource.basic.exception.BusinessException;
import org.cresource.basic.service.impl.BaseServiceImpl;
import org.cresource.order.service.IProductOrderService;
import org.cresource.pay.domain.AlipayInfo;
import org.cresource.pay.domain.PayBill;
import org.cresource.pay.mapper.AlipayInfoMapper;
import org.cresource.pay.mapper.PayBillMapper;
import org.cresource.pay.service.IPayBillService;
import org.cresource.pay.utils.AlipayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {
    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

//    @Autowired
//    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill payBill) {
        //判断是否生成支付单
        if(payBill == null){
            throw new BusinessException("请生成支付单后，在进行支付！！！");
        }
        PayBill payBill1 = payBillMapper.loadByUnionPaySn(payBill.getUnionPaySn());
        if(payBill1 == null){
            throw new BusinessException("请生成支付单后，在进行支付！！！");
        }
        //判断支付渠道
        Long payChannel = payBill1.getPayChannel();
        //防止为空  报错
        String resultData = "";
        //转换格式
        switch(payChannel.intValue()){
            case 1 ://支付宝
                //吧支付单cope到支付宝单
                AlipayInfo info = alipayInfoMapper.loadByShopId(payBill1.getShop_id());
                //跳转给支付宝
                resultData =  AlipayUtil.pay(info, payBill1);
                break;
            case 2 ://微信
                //TODO
                break;
            case 3 ://银联
                //TODO
                break;
            default : //0 余额
                //TODO
        }

        return resultData;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }

}
